package com.bdqn.controller;

import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysUserService;
import com.bdqn.util.Constants;
import com.bdqn.util.Page;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class SysUserController {
    private Logger logger = Logger.getLogger(SysUserController.class);
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

    //跳转到登录页面
    @RequestMapping(value = "/toLogin")
    public String login(){
        logger.info("跳转到登录页面");
        return "login";
    }

    //登录
    @PostMapping("/login")
    public String doLogin(HttpServletRequest request, HttpSession session, @RequestParam String account, @RequestParam String password){
        logger.debug("进入到登录方法");
        //调用service方法，进行用户匹配
        SysUser sysUser = sysUserService.login(account,password);
        if(null != sysUser){
            //将用户放入session
           session.setAttribute(Constants.USER_SESSION,sysUser);
            //登录成功，重定向到/user/toMain接口
            return "redirect:/user/toMain";
        }else {
            //用户不存在，登录失败，跳转到登录页面
            request.setAttribute("error","用户名或密码不正确");
            return "login";
        }
    }

    //跳转到首页
    @RequestMapping("/toMain")
    public String main(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION)==null){
            //重定向到登录页面接口
            return "redirect:/user/toLogin";
        }
        logger.info("跳转到首页");
        return "frame";
    }

    //捕获异常信息，跳转到error页面
    @ExceptionHandler(value = {RuntimeException.class})
    public String handlerException(RuntimeException e,HttpServletRequest request){
        request.setAttribute("e",e);
        return "error";
    }

    //一个抛出异常的接口
    @RequestMapping("/exLogin")
    public String exLogin(@RequestParam String account, @RequestParam String password){
        SysUser sysUser = sysUserService.login(account,password);
        if(null == sysUser){
            //登录失败
            throw new RuntimeException("用户名或密码不正确！");
        }
        return "redirect:/user/toMain";
    }

    //分页查询的接口
    @GetMapping("/list")
    public String getUserList(Model model,String queryRealName,Integer queryRoleId,@RequestParam(defaultValue = "1") Integer pageIndex){
        List<SysUser> userList = null;
        try{
            //设置页面容量
            int pageSize = Constants.pageSize;
            //总数量
            int totalCount = sysUserService.count(queryRealName,queryRoleId);
            //总页数
            Page page = new Page();
            page.setCurrPageNo(pageIndex);
            page.setPageSize(pageSize);
            page.setTotalCount(totalCount);
            int totalPageCount = page.getTotalPageCount();
            //控制首页和尾页
            if(pageIndex>totalCount){
                pageIndex = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            userList =sysUserService.selectPageList(pageIndex,pageSize,queryRealName,queryRoleId);
            model.addAttribute("userList",userList);
            List<SysRole> roleList = null;
            roleList = sysRoleService.selectList();
            model.addAttribute("roleList",roleList);
            model.addAttribute("queryRealName",queryRealName);
            model.addAttribute("queryRoleId",queryRoleId);
            model.addAttribute("totalPageCount",totalPageCount);
            model.addAttribute("totalCount",totalCount);
            model.addAttribute("currentPageNo",pageIndex);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("用户列表接口访问失败");
            return "redirect:/user/toSysError";
        }
        return "sysUser/list";
    }

    //跳转到异常处理页面
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }
}
